package com.hippy.erya.modular.erya;

import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.base.pojo.page.LayuiPageFactory;
import com.hippy.erya.base.pojo.page.LayuiPageInfo;
import com.hippy.erya.entity.CostingDO;
import com.hippy.erya.modular.form.excel.OrderDetailImport;
import com.hippy.erya.entity.OrderDetailDO;
import com.hippy.erya.exception.EyExceptionResult;
import com.hippy.erya.impl.OrderDetailServiceImpl;
import com.hippy.erya.modular.util.excel.EasyExcelUtils;
import com.hippy.erya.query.BaseQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.InputStream;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/erya/orderDetail")
public class OrderDetailController {

    @Autowired
    private OrderDetailServiceImpl orderDetailService;


    @RequestMapping(value = "/orderManager")
    public String orderManager() {
        return "/erya.orderdetail/orderdetail_setting.html";
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public LayuiPageInfo queryByPage(@PathParam("page") Long page, @PathParam("limit") int limit,
                                     @PathParam("orderChannel") String orderChannel,
                                     @PathParam("purchaseTimeRange") String purchaseTimeRange) {
        return LayuiPageFactory.createPageInfo(orderDetailService.queryByPage(page, limit, orderChannel, purchaseTimeRange));
    }

    @RequestMapping("/deleteData")
    @ResponseBody
    public ResponseData readMessage(String orderIds) {
        orderDetailService.deleteOrderDetail(orderIds);
        return ResponseData.success();
    }

    @RequestMapping("/uploadExcel")
    @ResponseBody
    public ResponseData uploadExcel(@RequestPart("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String userId = String.valueOf(LoginContextHolder.getContext().getUser().getId());
            EasyExcel.read(inputStream, OrderDetailImport.class, new ReadListener<OrderDetailImport>() {
                @Override
                public void invoke(OrderDetailImport data, AnalysisContext context) {
                    String orderCode = data.getOrderCode();
                    if (StringUtils.isEmpty(orderCode)) {
                        return;
                    }
                    OrderDetailDO orderDetailDO = new OrderDetailDO();
                    BeanUtils.copyProperties(data, orderDetailDO);
                    orderDetailDO.setMemberCode(data.getMemberPhone());
                    orderDetailDO.setOrderPlatform(convertOrderPlatform(orderDetailDO.getOrderPlatform()));
                    orderDetailDO.setCreatedBy(userId);
                    orderDetailDO.setUpdatedBy(userId);
                    orderDetailDO.setUserCode(userId);
                    orderDetailService.addOrderDetail(orderDetailDO);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {

                }
            }).sheet().doRead();;
        } catch (Exception e) {
            return ResponseData.error(EyExceptionResult.handleException(e));
        }
        return ResponseData.success(0, "上传成功", null);
    }

    private String convertOrderPlatform(String origin) {
        if ("美团".equals(origin)) {
            return "MT";
        }
        if ("饿了么".equals(origin)) {
            return "ELM";
        }
        return null;
    }

    @GetMapping("/downLoad")
    public void downLoad(HttpServletResponse response) {
        try {
            List<OrderDetailImport> orderDetailImports = Lists.newArrayList();
            EasyExcelUtils.write(orderDetailImports, OrderDetailImport.class, "订单数据.xlsx", "订单数据", response);
        } catch (Exception e) {
            log.error("平台数据文件下载失败", e);
        }
    }


}
